﻿@using NewLife;
@using NewLife.Web;
@using XCode;
@using System.Linq;
@{
    var page = ViewBag.Page as Pager;
    var efield = Model as EntityField;
    var id = Model as String;
    if (id.IsNullOrEmpty() && efield != null && efield.Field != null) id = efield.Field.MapField ?? efield.Field.Name;
    if (id.IsNullOrEmpty() && Model is DataField df) id = df.Name;
    if (id.IsNullOrEmpty()) id = "areaId";

    var areaId = 0;
    if (efield != null) areaId = efield.Entity[id].ToInt();
    if (areaId <= 0) areaId = page[id].ToInt();
    var areaIds = "";
    var area = Area.FindByID(areaId);
    if (area != null && area.ID > 0)
    {
        var ps = area.GetAllParents();
        ps.Add(area);
        areaIds = ps.Select(e => e.ID).Join("/") ?? "";
    }

    var set = NewLife.Cube.CubeSetting.Current;
    var res = set.ResourceUrl;
    if (String.IsNullOrEmpty(res)) res = "/Content";
    res = res.TrimEnd('/');

    var ver = typeof(ControllerBaseX).Assembly.GetName().Version;
}
<link href="@res/city_picker/city-picker.css" rel="stylesheet">
<div class="form-group" style="position:relative">
    <input readonly type="text" id="@(id)" value="@areaId" data-path="@areaIds" data-toggle="city-picker" placeholder="请选择省" data-level="province">
</div>
<script src="@res/city_picker/city-picker.js?v=@ver"></script>
<script>
    $.fn.citypicker.Constructor.DEFAULTS.dataUrl = '/Cube/AreaChilds?id=';
    $.fn.citypicker.Constructor.DEFAULTS.areaParents = '/Cube/AreaParents?id=';
</script>